using System;
using System.Web.UI.WebControls;
using Headspring.Training.Domain.Model;
using Headspring.Training.Domain.Services;
using Headspring.Training.WebUI.Presenters;
using Headspring.Training.WebUI.Views;
using StructureMap;

namespace Headspring.Training.WebUI
{
	public partial class SingleWorkOrder : PageBase, ISingleWorkOrderView
	{
		private WorkOrder _workOrder;
		private Employee[] _allEmployees;
		private IStateCommand[] _commandsToShow;
		private WorkOrderPresenter _presenter;
		private Employee _currentUser;

		protected void Page_Load(object sender, EventArgs e)
		{
			_presenter = ObjectFactory.FillDependencies<WorkOrderPresenter>();
			_presenter.Execute(this);

			if (!IsPostBack)
			{
				foreach (Employee employee in AllEmployees)
				{
					ddlAssignee.Items.Add(new ListItem(employee.GetFullName(), employee.UserName));
				}
				ddlAssignee.SelectedValue = CurrentUser.UserName;

				txtTitle.Text = WorkOrder.Title;
				txtDescription.Text = WorkOrder.Description;
				WorkOrderNumber.Value = WorkOrder.Number;
			}

			foreach (IStateCommand command in _commandsToShow)
			{
				Button button = new Button();
				button.UseSubmitBehavior = false;
				button.Text = command.TransitionVerbPresentTense;
				button.CommandName = command.TransitionVerbPresentTense;

				button.Command += Button_OnCommand;
				actionButtonsPane.Controls.Add(button);
			}

			ddlAssignee.Enabled = _presenter.UserCanChangeAssignee(this);

			if(_commandsToShow.Length == 0)
			{
				Label label = new Label();
				label.Text = "This work order is read-only for you at this time.";
				actionButtonsPane.Controls.Add(label);
				setReadOnly();
			}
		}

		private void setReadOnly()
		{
			txtTitle.Enabled = false;
			txtDescription.Enabled = false;
			ddlAssignee.Enabled = false;
		}

		public Employee CurrentUser
		{
			get { return _currentUser; }
			set { _currentUser = value; }
		}

		private void Button_OnCommand(object sender, CommandEventArgs e)
		{
			string commandName = e.CommandName;
			WorkOrder.Title = txtTitle.Text;
			WorkOrder.Description = txtDescription.Text;
			WorkOrder.Assignee = getSelectedUser();
			WorkOrder.Number = WorkOrderNumber.Value;
			_presenter.ExecuteCommand(this, commandName);
		}

		private Employee getSelectedUser()
		{
			Employee match = Array.Find(AllEmployees, delegate(Employee obj) { return obj.UserName == ddlAssignee.SelectedValue; });
			return match;
		}

		public Employee[] AllEmployees
		{
			get { return _allEmployees; }
			set { _allEmployees = value; }
		}

		public WorkOrder WorkOrder
		{
			get { return _workOrder; }
			set { _workOrder = value; }
		}

		public EditMode GetEditMode()
		{
			EditMode mode = EditMode.New;

			string rawMode = Request["mode"].Trim();
			if (!string.IsNullOrEmpty(rawMode))
			{
				mode = (EditMode) Enum.Parse(typeof (EditMode), rawMode, true);
			}

			return mode;
		}

		public string GetWorkOrderNumber()
		{
			string number = Request["wo_number"].Trim();
			return number;
		}

		public void SetCommandsToShow(IStateCommand[] commands)
		{
			_commandsToShow = commands;
		}

		public void Refresh()
		{
			Response.Redirect(Request.RawUrl);
		}
	}
}